{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# （1）栈",
   "id": "3ed0d25d8239c171"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-02T11:54:27.750244Z",
     "start_time": "2025-03-02T11:54:27.740370Z"
    }
   },
   "source": [
    "#创建class Stack\n",
    "class Stack:    \n",
    "\n",
    "#初始化栈\n",
    "    def __init__(self):\n",
    "        self.item=[]\n",
    "\n",
    "#判断栈是否为空\n",
    "    def isEmpty(self):\n",
    "        return len(self.items)==0\n",
    "\n",
    "#入栈\n",
    "    def push(self,item):\n",
    "        self.items.append(item)\n",
    "\n",
    "#出栈\n",
    "    def pop(self):\n",
    "        self.items.pop()\n",
    "        \n",
    "#返回栈顶的元素\n",
    "    def peek(self):\n",
    "        return self.items[len(self.items)-1]\n",
    "    \n",
    "#返回栈中元素的数目\n",
    "    def size(self):\n",
    "        return len(self.items)\n",
    "    \n"
   ],
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T11:54:30.248069Z",
     "start_time": "2025-03-02T11:54:30.238061Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#测试代码\n",
    "s=Stack()\n",
    "s.isEmpty"
   ],
   "id": "6b8f5f2956dc04ef",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Stack.isEmpty of <__main__.Stack object at 0x000001D4B665B440>>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T11:53:07.655323Z",
     "start_time": "2025-03-02T11:53:07.612310Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s.push('I')\n",
    "s.push('like')\n",
    "s.push('python')\n",
    "s.pop()\n",
    "s.peek()"
   ],
   "id": "ec1bd0dec2a10442",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Stack' object has no attribute 'items'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[11], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m s\u001B[38;5;241m.\u001B[39mpush(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mI\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m      2\u001B[0m s\u001B[38;5;241m.\u001B[39mpush(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mlike\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m      3\u001B[0m s\u001B[38;5;241m.\u001B[39mpush(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpython\u001B[39m\u001B[38;5;124m'\u001B[39m)\n",
      "Cell \u001B[1;32mIn[9], line 14\u001B[0m, in \u001B[0;36mStack.push\u001B[1;34m(self, item)\u001B[0m\n\u001B[0;32m     13\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mpush\u001B[39m(\u001B[38;5;28mself\u001B[39m,item):\n\u001B[1;32m---> 14\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mitems\u001B[38;5;241m.\u001B[39mappend(item)\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'Stack' object has no attribute 'items'"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T11:55:25.315638Z",
     "start_time": "2025-03-02T11:55:25.304580Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Stack:\n",
    "    def __init__(self):\n",
    "        self.items = []  # 初始化 items 为一个空列表\n",
    "\n",
    "    def push(self, item):\n",
    "        self.items.append(item)  # 将 item 添加到 items 列表中\n",
    "\n",
    "    def pop(self):\n",
    "        if not self.isEmpty():  # 确保栈不为空\n",
    "            return self.items.pop()\n",
    "        else:\n",
    "            raise IndexError(\"pop from empty stack\")\n",
    "\n",
    "    def isEmpty(self):\n",
    "        return len(self.items) == 0  # 检查栈是否为空"
   ],
   "id": "6bad4a35203a3760",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T11:55:37.602618Z",
     "start_time": "2025-03-02T11:55:37.589200Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s = Stack()  # 创建一个 Stack 对象\n",
    "s.push('I')  # 正常调用 push 方法\n",
    "s.push('like')\n",
    "s.push('python')\n",
    "\n",
    "print(s.pop())  # 输出：python\n",
    "print(s.pop())  # 输出：like\n",
    "print(s.pop())  # 输出：I\n",
    "print(s.isEmpty())  # 输出：True"
   ],
   "id": "d4d3bf11863bc2cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python\n",
      "like\n",
      "I\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 15
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
